//
//  NSMutableAttributedString+CoreText.m
//  DGCoreTextView
//
//  Created by Jinxiao on 13-1-11.
//  Copyright (c) 2013年 debugeek. All rights reserved.
//

#import "NSMutableAttributedString+DGCoreText.h"

@implementation NSMutableAttributedString (CoreText)

- (void)setParagraphWithLineBreakMode:(CTLineBreakMode)lineBreakMode textAlignment:(CTTextAlignment)textAlignment lineSpacingMin:(CGFloat)lineSpacingMin lineSpacingMax:(CGFloat)lineSpacingMax lineHeightMax:(CGFloat)lineHeightMax
{
    [self setParagraphWithLineBreakMode:lineBreakMode textAlignment:textAlignment lineSpacingMin:lineSpacingMin lineSpacingMax:lineSpacingMax lineHeightMax:lineHeightMax range:NSMakeRange(0, self.length)];
}

- (void)setParagraphWithLineBreakMode:(CTLineBreakMode)lineBreakMode textAlignment:(CTTextAlignment)textAlignment lineSpacingMin:(CGFloat)lineSpacingMin lineSpacingMax:(CGFloat)lineSpacingMax lineHeightMax:(CGFloat)lineHeightMax range:(NSRange)range
{
    CTParagraphStyleSetting alignmentSetting;
    CTParagraphStyleSetting lineBreakSetting;
    CTParagraphStyleSetting lineSpacingMinSetting;
    CTParagraphStyleSetting lineSpacingMaxSetting;
    CTParagraphStyleSetting lineHeightMaxSetting;
    
    alignmentSetting.spec = kCTParagraphStyleSpecifierAlignment;
    alignmentSetting.valueSize = sizeof(CTTextAlignment);
    alignmentSetting.value = (const void *)&textAlignment;
    
    lineBreakSetting.spec = kCTParagraphStyleSpecifierLineBreakMode;
    lineBreakSetting.valueSize = sizeof(CTLineBreakMode);
    lineBreakSetting.value = (const void *)&lineBreakMode;
    
    lineSpacingMinSetting.spec = kCTParagraphStyleSpecifierMinimumLineSpacing;
    lineSpacingMinSetting.valueSize = sizeof(float);
    lineSpacingMinSetting.value = (const void *)&lineSpacingMin;
    
    lineSpacingMaxSetting.spec = kCTParagraphStyleSpecifierMaximumLineSpacing;
    lineSpacingMaxSetting.valueSize = sizeof(float);
    lineSpacingMaxSetting.value = (const void *)&lineSpacingMax;
    
    
    lineHeightMaxSetting.spec = kCTParagraphStyleSpecifierMaximumLineHeight;
    lineHeightMaxSetting.valueSize = sizeof(float);
    lineHeightMaxSetting.value = (const void *)&lineHeightMax;
    
    CTParagraphStyleSetting paraStyles[5] =
    {
        alignmentSetting, lineBreakSetting, lineSpacingMinSetting, lineSpacingMaxSetting, lineHeightMaxSetting
	};
    
	CTParagraphStyleRef styleRef = CTParagraphStyleCreate(paraStyles, 5);
    [self removeAttribute:(NSString *)kCTParagraphStyleAttributeName range:range];
	[self addAttribute:(NSString *)kCTParagraphStyleAttributeName value:(__bridge id)styleRef range:range];
	CFRelease(styleRef);
}

- (void)setTextFont:(UIFont *)textFont
{
    [self setTextFont:textFont range:NSMakeRange(0, self.length)];
}

- (void)setTextFont:(UIFont *)textFont range:(NSRange)range
{
    [self removeAttribute:(NSString *)kCTFontAttributeName range:range];
    
    if(textFont)
    {
        CTFontRef fontRef = CTFontCreateWithName((CFStringRef)textFont.fontName, textFont.pointSize, NULL);
        [self addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)fontRef range:range];
        CFRelease(fontRef);
    }
}

- (void)setTextColor:(UIColor *)textColor
{
    [self setTextColor:textColor range:NSMakeRange(0, self.length)];
}

- (void)setTextColor:(UIColor *)textColor range:(NSRange)range
{
    [self removeAttribute:(NSString *)kCTForegroundColorAttributeName range:range];
    
    if(textColor)
    {
        [self addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)textColor.CGColor range:range];
    }
}

- (void)setPattern:(NSDictionary *)pattern range:(NSRange)range
{
    if(pattern)
    {
        [self addAttributes:pattern range:range];
    }
}

@end
